<!--author:蒋鑫-->
<!--data:2021/3/5-->
<!--下载资源-->
<div class="layui-card">
    <div class="layui-card-header">
        <h2 class="header-title">文件中心</h2>
        <span class="layui-breadcrumb pull-right">
          <a href="#!home_console">首页</a>
          <a><cite>文件中心</cite></a>
        </span>
    </div>
    <div class="layui-card-body">
        <div class="layui-form toolbar">
            搜索：<input class="layui-input search-input" id="finame" name="finame" placeholder="请输入文件名" type="text"/>&emsp;
            <button class="layui-btn" id="resourcesSearch" lay-filter="data-search-btn" lay-submit
                    type="text"><i
                    class="layui-icon"></i> 搜 索
            </button>
        </div>

        <!-- 数据表格 -->
        <table class="layui-table" id="files-table" lay-filter="files-table"></table>
    </div>
</div>

<!-- 表格操作列 -->
<script id="files-table-bar" type="text/html">
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="download">下载</a>
</script>


<script>
    layui.use(['form', 'table', 'util', 'config', 'admin'], function () {
        let form = layui.form;
        let table = layui.table;
        let config = layui.config;
        let layer = layui.layer;
        let util = layui.util;
        let admin = layui.admin;


        // 渲染表格
        table.render({
            elem: '#files-table',
            url: config.base_server + 'api-forum/resources/selectAll',
            method: 'GET',
            where: {
                access_token: config.getToken().access_token
            },
            page: true,
            cols: [[
                {field: 'fileid', sort: true, title: '编号', width: 80},
                {field: 'username', sort: true, title: '作者'},
                {field: 'finame', sort: true, title: '资源名'},
                {field: 'fiinfo', sort: true, title: '资源描述'},
                {field: 'fitype', sort: true, title: '文件类型'},
                {field: 'catgry', sort: true, title: '资源分类'},
                {field: 'fitags', sort: true, title: '资源标签'},
                {field: 'fisize', minWidth: 120, sort: true, title: '文件大小(B)'},
                {field: 'fcount', sort: true, title: '下载次数'},
                {field: 'points', sort: true, title: '下载所需积分'},
                {field: 'cretim', sort: true, title: '上传时间'},
                {field: 'updtim', sort: true, title: '更新时间'},
                {field: 'filurl', width: 250, align: 'center', title: '内容'},
                {align: 'center', width: 80, toolbar: '#files-table-bar', title: '下载'}
            ]]
        });

        //显示表单弹窗
        // let showEditModel = function (data) {
        //     //将表格某一列的值存储起来，用于弹出框的回显
        //     admin.putTempData('download_detail', data);
        //     let path = 'pages/forum/downloadMessage.html';
        //     admin.popupCenter({
        //         title: "资源下载详情",
        //         path: path,
        //         finish: function () {
        //             table.reload('user-table', {});
        //         }
        //     });
        // };

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            //执行搜索重载
            table.reload('files-table', {
                page: {
                    curr: 1//从第一页开始搜索
                }
                , where: data.field

            }, 'data');
            return false;
        });

        // 工具条点击事件
        table.on('tool(files-table)', function (obj) {
            let data = obj.data;
            let layEvent = obj.event;

            let userid = config.getUser().id;
            let resourceBuyRecord = {
                'userid': userid,
                'fileid': data.fileid,
                'points': data.points
            }
            console.log(resourceBuyRecord, 666666666)
            if (layEvent === 'download') { // 下载
                admin.req('api-forum/resources/downloadResource', JSON.stringify(resourceBuyRecord), function (result) {
                    if (result.code == 0) {
                        // 创建a标签
                        let a = document.createElement('a')
                        // 定义下载名称
                        a.download = data.finame;
                        // 隐藏标签
                        a.style.display = 'none';
                        // 设置文件路径
                        a.href = data.filurl;
                        // 将创建的标签插入dom
                        document.body.appendChild(a)
                        // 点击标签，执行下载
                        a.click()
                        // 将标签从dom移除
                        document.body.removeChild(a)
                        if (result.msg == "购买成功") {
                            admin.req('api-forum/resources/buyRecords', JSON.stringify(resourceBuyRecord), function (e) {

                            }, 'POST');
                        }
                    }
                }, 'POST');
            }
        });


        // 搜索按钮点击事件
        $('#files-btn-search').click(function () {
            var key = $('#files-edit-search').val();
            table.reload('files-table', {where: {searchKey: key}});
        });
    });


</script>
